package com.andnux.android.utils;

import java.lang.reflect.Field;

public class ReflectUtils {

    public static <M> void savaValue(M object, String key, Object value) {
        try {
            Field declaredField = object.getClass().getDeclaredField(key);
            declaredField.setAccessible(true);
            if (declaredField.getType().isAssignableFrom(String.class)) {
                declaredField.set(object, value.toString());
            } else if (declaredField.getType().isAssignableFrom(int.class) || declaredField.getType().isAssignableFrom(Integer.class)) {
                declaredField.set(object, StringUtils.toInt(value));
            } else if (declaredField.getType().isAssignableFrom(double.class) || declaredField.getType().isAssignableFrom(Double.class)) {
                declaredField.set(object, StringUtils.toDouble(value));
            } else if (declaredField.getType().isAssignableFrom(float.class) || declaredField.getType().isAssignableFrom(Float.class)) {
                declaredField.set(object, StringUtils.toFloat(value));
            } else {
                declaredField.set(object, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
